package org.jeecg.modules.inspect.service.impl;

import org.jeecg.modules.inspect.entity.InsClasses;
import org.jeecg.modules.inspect.entity.InsClassesLine;
import org.jeecg.modules.inspect.entity.InsClassesUser;
import org.jeecg.modules.inspect.entity.InsClassesCommon;
import org.jeecg.modules.inspect.entity.InsClassesSpecial;
import org.jeecg.modules.inspect.mapper.InsClassesLineMapper;
import org.jeecg.modules.inspect.mapper.InsClassesUserMapper;
import org.jeecg.modules.inspect.mapper.InsClassesCommonMapper;
import org.jeecg.modules.inspect.mapper.InsClassesSpecialMapper;
import org.jeecg.modules.inspect.mapper.InsClassesMapper;
import org.jeecg.modules.inspect.service.IInsClassesService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 班次管理
 * @Author: jeecg-boot
 * @Date:   2024-03-10
 * @Version: V1.0
 */
@Service
public class InsClassesServiceImpl extends ServiceImpl<InsClassesMapper, InsClasses> implements IInsClassesService {

	@Autowired
	private InsClassesMapper insClassesMapper;
	@Autowired
	private InsClassesLineMapper insClassesLineMapper;
	@Autowired
	private InsClassesUserMapper insClassesUserMapper;
	@Autowired
	private InsClassesCommonMapper insClassesCommonMapper;
	@Autowired
	private InsClassesSpecialMapper insClassesSpecialMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(InsClasses insClasses, List<InsClassesLine> insClassesLineList,List<InsClassesUser> insClassesUserList,List<InsClassesCommon> insClassesCommonList,List<InsClassesSpecial> insClassesSpecialList) {
		insClassesMapper.insert(insClasses);
		if(insClassesLineList!=null && insClassesLineList.size()>0) {
			for(InsClassesLine entity:insClassesLineList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesLineMapper.insert(entity);
			}
		}
		if(insClassesUserList!=null && insClassesUserList.size()>0) {
			for(InsClassesUser entity:insClassesUserList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesUserMapper.insert(entity);
			}
		}
		if(insClassesCommonList!=null && insClassesCommonList.size()>0) {
			for(InsClassesCommon entity:insClassesCommonList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesCommonMapper.insert(entity);
			}
		}
		if(insClassesSpecialList!=null && insClassesSpecialList.size()>0) {
			for(InsClassesSpecial entity:insClassesSpecialList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesSpecialMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(InsClasses insClasses,List<InsClassesLine> insClassesLineList,List<InsClassesUser> insClassesUserList,List<InsClassesCommon> insClassesCommonList,List<InsClassesSpecial> insClassesSpecialList) {
		insClassesMapper.updateById(insClasses);
		
		//1.先删除子表数据
		insClassesLineMapper.deleteByMainId(insClasses.getId());
		insClassesUserMapper.deleteByMainId(insClasses.getId());
		insClassesCommonMapper.deleteByMainId(insClasses.getId());
		insClassesSpecialMapper.deleteByMainId(insClasses.getId());
		
		//2.子表数据重新插入
		if(insClassesLineList!=null && insClassesLineList.size()>0) {
			for(InsClassesLine entity:insClassesLineList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesLineMapper.insert(entity);
			}
		}
		if(insClassesUserList!=null && insClassesUserList.size()>0) {
			for(InsClassesUser entity:insClassesUserList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesUserMapper.insert(entity);
			}
		}
		if(insClassesCommonList!=null && insClassesCommonList.size()>0) {
			for(InsClassesCommon entity:insClassesCommonList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesCommonMapper.insert(entity);
			}
		}
		if(insClassesSpecialList!=null && insClassesSpecialList.size()>0) {
			for(InsClassesSpecial entity:insClassesSpecialList) {
				//外键设置
				entity.setClassId(insClasses.getId());
				insClassesSpecialMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		insClassesLineMapper.deleteByMainId(id);
		insClassesUserMapper.deleteByMainId(id);
		insClassesCommonMapper.deleteByMainId(id);
		insClassesSpecialMapper.deleteByMainId(id);
		insClassesMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			insClassesLineMapper.deleteByMainId(id.toString());
			insClassesUserMapper.deleteByMainId(id.toString());
			insClassesCommonMapper.deleteByMainId(id.toString());
			insClassesSpecialMapper.deleteByMainId(id.toString());
			insClassesMapper.deleteById(id);
		}
	}
	
}
